package com.fenghuajueli.module_user.activity.forget;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.constants.TypeConstant;
import com.fenghuajueli.module_user.databinding.ActivityForgetPasswordBinding;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_user.model.UserViewModel;

@Route(path = UserModuleRoute.USER_FORGET_PASSWORD)
public class ForgetPasswordActivity extends UserViewModelBaseActivity implements View.OnClickListener {

    private boolean phoneIsInputComplete = false;

    ActivityForgetPasswordBinding binding;

    private final UserViewModel userViewModel = new UserViewModel();

    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityForgetPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.etInputPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    phoneIsInputComplete = false;
                } else {
                    phoneIsInputComplete = true;
                }
                changeButtonStatus();
            }
        });
        binding.btnGoNext.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
        userViewModel.phoneIsRegister.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean isRegister) {
                if (!isRegister) {
                    ARouter.getInstance().build(UserModuleRoute.USER_SEND_CODE_ACTIVITY)
                            .withString("phone", binding.etInputPhone.getText().toString().trim())
                            .withString("type", TypeConstant.VerCodeType.FORGIVE.name())
                            .navigation();
                } else {
                    ToastUtils.showShort("该手机号未注册！");
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ivClose) {
            finish();
        } else if (view.getId() == R.id.btnGoNext) {
            checkPhone(binding.etInputPhone.getText().toString().trim());
        }
    }


    private void checkPhone(String phone) {
        if (userViewModel != null) {
            userViewModel.checkPhoneIsRegister(this, phone);
        }
    }


    private void changeButtonStatus() {
        if (phoneIsInputComplete) {
            binding.btnGoNext.setBackgroundResource(R.drawable.shape_btn_can_use_bg);
            binding.btnGoNext.setEnabled(true);
        } else {
            binding.btnGoNext.setBackgroundResource(R.drawable.shape_btn_not_use_bg);
            binding.btnGoNext.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}